package com.joshua.userservice;

import org.hibernate.*;
import org.springframework.beans.factory.annotation.Autowired;

public class HibernateUtil
{
	@Autowired
	public SessionFactory sessionFactory;
	
	public final ThreadLocal<Session> session
		= new ThreadLocal<Session>();
	
	public Session currentSession()
		throws HibernateException
	{
		Session s = session.get();
		if (s == null)
		{
			s = sessionFactory.openSession();
			session.set(s);
		}
		return s;
	}
	
	public void closeSession()
		throws HibernateException 
	{
		Session s = session.get();
		if (s != null)
			s.close();
		session.set(null);
	}
}